package com.ptit.giaothong.bienbao;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.ptit.giaothong.DelayActivity;
import com.ptit.giaothong.GlobalData;
import com.ptit.giaothong.MainActivity;
import com.ptit.giaothong.R;
import com.ptit.giaothong.database.DatabaseHelper;

public class BienbaoActivity {

	private ListView listview;
	private Activity activity;

	public BienbaoActivity(Activity activity) {
		this.activity = activity;
	}

	public void init(int keypage) {
		MainActivity.keypage = keypage;
		activity.setContentView(R.layout.bienbao);

		TextView title = (TextView) activity.findViewById(R.id.textView1);
		title.setText("Biển báo giao thông");
		if (DelayActivity.listBienbao == null
				|| DelayActivity.listBienbao.size() == 0) {
			DelayActivity.listBienbao = new ArrayList<Itembb>();
			Cursor c = DelayActivity.myDbHelper.query(
					DatabaseHelper.TB_BIENBAO, null, null, null, null, null,
					null);
			if (c.moveToFirst()) {
				do {
					Itembb item = new Itembb();
					item.setCategorId(c.getInt(1));
					item.setIcon(c.getString(2));
					item.setDesc(c.getString(3));
					item.setName(c.getString(4));
					item.setSohieu(c.getInt(5));
					DelayActivity.listBienbao.add(item);
				} while (c.moveToNext());
			}

		}
		String[] listLoaibien = activity.getResources().getStringArray(
				R.array.listloaibien);
		Spinner spinner = (Spinner) activity.findViewById(R.id.spinner1);
		GlobalData.setAdapterSpinner(activity,spinner, listLoaibien);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		listview = (ListView) activity.findViewById(R.id.listViewbb);
		ItemAdapter itemAdapter = new ItemAdapter(activity,
				R.layout.itembienbao, DelayActivity.listBienbao);
		listview.setAdapter(itemAdapter);

	}

	

	class ItemAdapter extends ArrayAdapter<Itembb> {
		private int resource;
		private LayoutInflater layoutInflater;

		public ItemAdapter(Context mContext, int resource, List<Itembb> models) {
			super(mContext, resource, models);
			this.resource = resource;
			layoutInflater = LayoutInflater.from(mContext);
		}

		public class ViewHolder {
			public TextView desc;
			public TextView name;
			public LinearLayout linearLayout;
			public ImageView thumnail;

		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = layoutInflater.inflate(resource, null, true);
				viewHolder.desc = (TextView) convertView
						.findViewById(R.id.txtdesc);
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.name);
				viewHolder.linearLayout = (LinearLayout) convertView
						.findViewById(R.id.linebienbao);

				viewHolder.thumnail = (ImageView) convertView
						.findViewById(R.id.imgthumid);

				convertView.setTag(viewHolder);

			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			final Itembb item = getItem(position);
			Log.d("item.getIcon()", item.getIcon());
			viewHolder.name.setText("Tên biển báo: " + item.getName());
			viewHolder.desc.setText("Số hiệu biển: " + item.getSohieu());

			viewHolder.thumnail.setImageResource(activity.getResources()
					.getIdentifier(item.getIcon(), "drawable",
							activity.getPackageName()));
			viewHolder.linearLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Bienbaodetail itemBienbaodetail = new Bienbaodetail(
							activity);
					itemBienbaodetail.Init(item);

				}
			});
			return convertView;
		}
	}

}
